﻿#include "PrimitiveGenerator.h"

namespace Kriko
{
    PrimitiveData PrimitiveGenerator::CreateRect(float width, float height, size_t numSubdivisions)
    {
        PrimitiveData data;

        float w = 0.5f * width;
        float h = 0.5f * height;
        
        glm::vec4 vertices[4];
        glm::vec2 uvs[4];
        glm::vec3 normals[4];

        // 0
        vertices[0] = glm::vec4(-w, h, 0.0f, 0.0f);
        uvs[0] = glm::vec2(0.0f, 0.0f);
        normals[0] = glm::vec3(0.0f, 0.0f, 1.0f);

        vertices[1] = glm::vec4(w, h, 0.0f, 0.0f);
        uvs[1] = glm::vec2(1.0f, 0.0f);
        normals[1] = glm::vec3(0.0f, 0.0f, 1.0f);

        vertices[2] = glm::vec4(-w, -h, 0.0f, 0.0f);
        uvs[2] = glm::vec2(0.0f, 1.0f);
        normals[2] = glm::vec3(0.0f, 0.0f, 1.0f);

        vertices[3] = glm::vec4(w, -h, 0.0f, 0.0f);
        uvs[3] = glm::vec2(1.0f, 1.0f);
        normals[3] = glm::vec3(0.0f, 0.0f, 1.0f);

        data.vertices.assign(&vertices[0], &vertices[4]);
        data.uvs.assign(&uvs[0], &uvs[4]);
        data.normals.assign(&normals[0], &normals[4]);

        int i[6];
        i[0] = 0;
        i[1] = 1;
        i[2] = 2;
        i[3] = 2;
        i[4] = 1;
        i[5] = 3;
        data.indices.assign(&i[0], &i[6]);

        return data;
    }

    void PrimitiveGenerator::Subdivide(PrimitiveData& data)
    {
        PrimitiveData copy = data;
        data.vertices.resize(0);
        data.uvs.resize(0);
        data.normals.resize(0);
        data.tangents.resize(0);
        data.indices.resize(0);

        // 细分
        size_t nums = copy.indices.size() / 3;
        for (size_t i = 0; i < nums; ++i)
        {
            
        }
    }

}